﻿#region License

/*
 © 2012 Alex Kazaev
 * FolderDrive project [http://folderdrive.codeplex.com/]
 * Licensed under Ms-PL license [http://www.opensource.org/licenses/MS-PL]
 */

#endregion License
using System.Diagnostics;

namespace FolderDrive.Core
{
    /// <summary>
    /// Class that used to manage external process that needs to be elevated in order to work with registry.
    /// </summary>
    public static class FolderRegistryProcess
    {
        const string UtilityName = "FolderDriveRegistryUtility.exe";
        /// <summary>
        /// Creates elevated process and waits until it exits.
        /// </summary>
        /// <param name="args">Process arguments</param>
        /// <returns>true on success; otherwise false</returns>
        private static bool CreateProcess(string args)
        {
            try
            {
                ProcessStartInfo processInfo = new ProcessStartInfo();
                processInfo.Verb = "runas";
                processInfo.FileName = UtilityName;
                processInfo.Arguments = args;
                processInfo.WindowStyle = ProcessWindowStyle.Hidden;
                Process p = Process.Start(processInfo);
                p.WaitForExit();
                return p.ExitCode == 0;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// Creates the drive mapping
        /// </summary>
        /// <param name="drive">drive letter</param>
        /// <param name="path">mapping path</param>
        /// <returns>true on success; otherwise false</returns>
        public static bool AddDrive(string drive, string path)
        {
            return CreateProcess(drive + " " + path);
        }

        /// <summary>
        /// Removes the drive mapping
        /// </summary>
        /// <param name="drive">the drive letter</param>
        /// <returns>true on success; otherwise false</returns>
        public static bool RemoveDrive(string drive)
        {
           return CreateProcess(drive);
        }
    }
}
